﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    public class DealerDataAccess : BaseDataAccess<Business.Entities.Dealer>
    {
        public override GMAC.ReportingSystem.Business.Entities.Dealer Get(object id)
        {
            ArrayList parameters = new ArrayList() { id };
            DataTable dt = DataAccessComponent.ExecuteSelect("procDealerByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        public DealerCollection GetList()
        {
            DataTable dt = DataAccessComponent.ExecuteSelect("procDealerSelect", null);
            return ReadTable(dt) as DealerCollection;
        }

        protected override GMAC.ReportingSystem.Business.Entities.Dealer ReadRow(System.Data.DataRow row)
        {
            Dealer Dealer = null;
            if (row != null)
            {
                try
                {
                    Dealer = new Dealer();

                    Dealer.DealerID            = (int)row["DealerID"];
                    Dealer.DealerNumber        = row["DealerNumber"].ToString();
                    Dealer.DealerName          = row["DealerName"].ToString();
                    Dealer.DealerTaxID         = row["DealerTaxID"].ToString();
                    Dealer.DealerPhoneNumber   = row["DealerPhoneNumber"].ToString();
                    Dealer.DealerFaxNumber     = row["DealerFaxNumber"].ToString();
                    
                    Dealer.DealerElectronicMailAddress = row["DealerElectronicMailAddress"].ToString();
                    
                    Dealer.Status              = bool.Parse(row["Status"].ToString());
                    Dealer.DealerAddressLine1  = row["DealerAddressLine1"].ToString();
                    Dealer.DealerAddressLine2  = row["DealerAddressLine2"].ToString();
                    Dealer.DealerAddressLine3  = row["DealerAddressLine3"].ToString();
                    Dealer.DealerAddressLine4  = row["DealerAddressLine4"].ToString();
                    Dealer.DealerSectionNumber = row["DealerSectionNumber"].ToString();

                    Dealer.DealerFI = bool.Parse(row["DealerFI"].ToString());
                    
                    StateDataAccess sda = new StateDataAccess();
                    
                    if (!string.IsNullOrEmpty(row["DealerState"].ToString()))
                    {
                        Dealer.DealerState = sda.Get(row["DealerState"].ToString());
                    }
                    else
                    {
                        Dealer.DealerState = null;
                    }
                    if (!string.IsNullOrEmpty(row["DealerType"].ToString()))
                        Dealer.DealerType = (GeneralEnumTypes.TypeDealer)Enum.Parse(typeof(GeneralEnumTypes.TypeDealer), (row["DealerType"].ToString())); 

                    Dealer.DealerGlink = bool.Parse(row["DealerGlink"].ToString());

                    AccountRepresentativeDataAccess ar = new AccountRepresentativeDataAccess();
                    
                    if (!string.IsNullOrEmpty(row["AccountRepresentativeID"].ToString()))
                    {
                        Dealer.AccountRepresentativeID = ar.Get(row["AccountRepresentativeID"].ToString());
                    }
                    else
                    {
                        Dealer.AccountRepresentativeID = null;
                    }

                    DealerGroupDataAccess dg = new DealerGroupDataAccess();
                    if (!string.IsNullOrEmpty(row["DealerGroupID"].ToString()))
                    {
                        Dealer.DealerGroupID = dg.Get(row["DealerGroupID"].ToString());
                    }
                    else
                    {
                        Dealer.DealerGroupID = null;
                    }

                }
                catch (Exception)
                {
                    throw;
                }
            }
            return Dealer;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.Dealer> ReadTable(System.Data.DataTable table)
        {
            try
            {
                DealerCollection Dealers = new DealerCollection();
                Dealer Dealer;
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        Dealer = ReadRow(row);
                        if (Dealer != null)
                        {
                            Dealers.Add(Dealer);
                        }
                    }
                }
                return Dealers;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DealerCollection Search(string Dealername, string DealerNumber, PagingDetails paging)
        {
            ArrayList parameters = new ArrayList() { Dealername, DealerNumber, paging.PageNumber, paging.PageSize };

            try
            {
                DataSet ds = DataAccessComponent.ExecuteMultipleSelect("procDealerByFilterSelect", parameters);
                DealerCollection result = null;
                if (ds.Tables.Count == 2)
                {
                    result = ReadTable(ds.Tables[0]) as DealerCollection;
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        result.TotalResults = (int)ds.Tables[1].Rows[0][0];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }


    }
}
